
package com.probiz.estoresa.sales.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.service.GiftCategoryManager;
import com.probiz.estore.common.model.catalog.GiftCategory;
import com.probiz.estore.common.model.sales.Gift;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.sales.model.GiftSearchCriteria;
import com.probiz.estore.sales.service.GiftManager;

public class giftSelectorController extends GenericStoreAdminController<Gift> {

	private GiftManager			giftManager			= null;
	private GiftCategoryManager	giftCategoryManager	= null;

	public void setGiftManager(GiftManager giftManager) {
		this.giftManager = giftManager;
	}

	public void setGiftCategoryManager(GiftCategoryManager giftCategoryManager) {
		this.giftCategoryManager = giftCategoryManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericStoreAdminController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Gift entity) {
		return entity.getGiftName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericStoreAdminController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = giftManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, Gift entity,	BindException errors) {

	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("sales/giftSelector");
		GiftCategory treeRoot = giftCategoryManager.getById(1);
		Set<GiftCategory> categorySet = treeRoot.getChildren();
		List<GiftCategory> categoryList = new ArrayList<GiftCategory>();
		categoryList.addAll(categorySet);
		mv.addObject("categoryList", categoryList);
		mv.addObject("treeRoot", treeRoot);
		return mv;
	}

	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("sales/giftSelectorResults");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		GiftSearchCriteria giftSearchCriteria = new GiftSearchCriteria();
		try {
			// 绑定参数
			bind(request, giftSearchCriteria);
			List<Gift> giftList = giftManager.searchByCriteria(
					giftSearchCriteria, searchCriteria);
			mv.addObject("giftList", giftList);
			mv.addObject("giftSearchCriteria", giftSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;

	}

}
